
<?php

class Admin_Controllers_Product extends Libs_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->view->render('product/index');
    }

    public function getExtension($str) {
        $i = strrpos($str, ".");
        if (!$i) {
            return "";
        }
        $l = strlen($str) - $i;
        $ext = substr($str, $i + 1, $l);
        return $ext;
    }

     public function checkUploadImage($array) {
        define("MAX_SIZE", "1000");
        $flag = false;
        $array = $_FILES['image'];
        $error = $_FILES['image']['error'];
        $image_name = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        if ($image_name) {
            $filename = stripslashes($_FILES['image']['name']);
            $extension = $this->getExtension($filename);
            $extension = strtolower($extension);
            if (($extension != "jpg") && ($extension != "png") && ($extension != "jpeg") && ($extension != "gif")) {
                echo "<script>alert('Đây không phải là file ảnh, mời bạn chọn file khác')</script>";
                $flag = false;
            } else {
                $size = $_FILES['image']['size'];
                if ($size > MAX_SIZE * 1024) {
                    echo "<script>alert('Vượt quá dung lượng cho phép')</script>";
                    $flag = false;
                } else {
                    if ($error == 0) {
                        if (!file_exists("templates/admin/images/product/" . $image_name)) {
                            move_uploaded_file($tmp_name, "templates/admin/images/product/" . $image_name);
                            $flag = true;
                        } else {
                            echo "<script>alert('Đã tồn tại file,mời bạn chọn file khác')</script>";
                            $flag = false;
                        }
                    }
                }
            }
        }
        return $flag;
    }


    public function AddProduct() {

        $this->view->render('product/add-product');
    }

    public function getDateNow() {
        $time = getdate();
        $dateNow = $time['year'] . '-' . $time['mon'] . '-' . $time['mday'];
        return $dateNow;
    }
      public function SaveAdd() {
        $product = new Admin_Model_Product();
        if (isset($_POST['Add'])) {
            $image = $_FILES['image'];
            $check = $this->checkUploadImage($image);
            if ($check == 1) {
                $name_product = $_POST['txtNameProduct'];
                $price_old = $_POST['txtPriceOld'];
                $price = $_POST['txtPriceNew'];
                $image_url = 'images/product/' . $_FILES['image']['name'];
                $number = $_POST['txtNumber'];
                $date = $this->getDateNow();
                $cate = $_POST['group'];
                $cateS = $_POST['groupSmall'];
                $data = array('pro_name' => $name_product, 'pro_price_old' => $price_old, 'pro_price' => $price, 'pro_img' => $image_url, 'pro_number' => $number, 'pro_date' => $date, 'cate_id' => $cate, 'cates_id' => $cateS);
                $resultInsert = $product->InsertProduct($data);
                if ($resultInsert == true) {
                    $pro_id = mysql_insert_id();
                    $detail1 = $_POST['txtdetail1'];
                    $detail2 = $_POST['txtdetail2'];
                    $detail3 = $_POST['txtdetail3'];
                    $sub = $_POST['txtSub'];
                    $weight = $_POST['txtW'];
                     $color = $_POST['txtColor'];
                     $color_detail = $_POST['txtColorDetail'];
                     $size = $_POST['txtSize'];
                     $sizedetail = $_POST['txtSizeDetail'];
                     $styledetail = $_POST['txtStyleDetail'];
                     $product_detail = new Admin_Model_ProductDetail();
                   $mang = array('id'=>$pro_id,'detail_short1'=>$detail1,'detail_short2'=>$detail2,'detail_short3'=>$detail3,'substance'=>$sub,'weight'=>$weight,'color_name'=>$color,'size_name'=>$size,'style_detail'=>$styledetail,'color_detail'=>$color,'size_detail'=>$sizedetail);
                   $product_detail->InsertProductDetail($mang);
                    $_SESSION['checkInsert'] = 1;
                    header("location:index");
                } else {
                    $_SESSION['checkInsert'] = 2;
                    header("location:index");
                }
            }
        }
        if (isset($_POST['Edit'])) {
            $id = $_POST['txtProID'];
            $name_product = $_POST['txtNameProduct'];
            $price_old = $_POST['txtPriceOld'];
            $price = $_POST['txtPriceNew'];
            $image = $_FILES['image'];
            $check = $this->checkUploadImage($image);
            if ($check == 1) {
                $image_url = 'images/product/' . $_FILES['image']['name'];
            }
            $number = $_POST['txtNumber'];
            $date = $this->getDateNow();
            $cate = $_POST['group'];
            $cateS = $_POST['groupSmall'];
            $data = array('pro_name' => $name_product, 'pro_price_old' => $price_old, 'pro_price' => $price, 'pro_img' => $image_url, 'pro_number' => $number, 'pro_date' => $date, 'cate_id' => $cate, 'cates_id' => $cateS);
            $resultUpdate = $product->UpdateProduct($data, $id);
            if ($resultUpdate == true) {
                $_SESSION['checkUpdate'] = 1;
                header("location:index");
            } else {
                $_SESSION['checkUpdate'] = 2;
                header("location:index");
            }
        }
    }
    public function delPro(){
        $product = new Admin_Model_Product();
        if(isset($_GET['pro_id'])){
            $id = $_GET['pro_id'];
            $resultDelete = $product->deleteProduct($id);
            $resultDelete2 = $product->deleteDetailProduct($id);
            if ($resultDelete == true && $resultDelete2 == true) {
                $_SESSION['checkDelete'] = 1;
                header("location:index");
            } else {
                $_SESSION['checkDelete'] = 2;
                header("location:index");
            }
        }
    }
    public function showProductDetail(){
         $this->view->render('product/show-detail');
    }

}

?>
